public class short[]
extends Object
GDK enhancements for short[].
| Type Params | Return Type | Name and description |
|---|---|---|
|
public boolean |
any(Closure<?> predicate)Iterates over the contents of a short Array, and checks whether a predicate is valid for at least one element. |
|
public boolean |
asBoolean()Coerces a short array to a boolean value. |
|
public BigDecimal |
average()Calculates the average of the shorts in the array. |
|
public List<List<Short>> |
chop(int chopSizes)Chops the short array into pieces, returning lists with sizes corresponding to the supplied chop sizes. |
|
public boolean |
contains(Object value)Checks whether the array contains the given value. |
|
public Number |
count(Object value)Counts the number of occurrences of the given value inside this array. |
|
public short[] |
each(Consumer<Short> consumer)Iterates through a short[] passing each short to the given consumer. |
|
public short[] |
eachWithIndex(Closure<?> closure)Iterates through a short[], passing each short and the element's index (a counter starting at zero) to the given closure. |
|
public boolean |
equals(short[] right)Compares the contents of this array to the contents of the given array. |
|
public boolean |
every(Closure<?> predicate)Iterates over the contents of a short Array, and checks whether a predicate is valid for all elements. |
|
public short |
first()Returns the first item from the short array. |
|
public List<Short> |
flatten()Flattens an array. |
|
public List<Short> |
getAt(Range<?> range)Supports the subscript operator for a short array with a range giving the desired indices. |
|
public List<Short> |
getAt(IntRange range)Supports the subscript operator for a short array with an IntRange giving the desired indices. |
|
public List<Short> |
getAt(ObjectRange range)Supports the subscript operator for a short array with an ObjectRange giving the desired indices. |
|
public List<Short> |
getAt(Collection<?> indices)Supports the subscript operator for a short array with a (potentially nested) collection giving the desired indices. |
|
public IntRange |
getIndices()Returns indices of the short array. |
|
public String |
groovyToString()Returns Groovy's list-like string representation for a short array. |
|
public short |
head()Returns the first item from the short array. |
|
public short[] |
init()Returns the items from the short array excluding the last item. |
|
public String |
join()Concatenates the string representation of each item in this array. |
|
public String |
join(String separator)Concatenates the string representation of each item in this array, with the given String as a separator between each item. |
|
public short |
last()Returns the last item from the short array. |
|
public int |
partitionPoint(IntRange range, IntPredicate condition)Returns the index of the partition point according to the given predicate (the index of the first element of the second partition). |
|
public int |
partitionPoint(IntPredicate condition)Returns the index of the partition point according to the given predicate (the index of the first element of the second partition). |
|
public void |
putAt(IntRange range, short[] source)A helper method to allow arrays to be set with subscript operators. |
|
public void |
putAt(IntRange range, Iterable<Number> source)A helper method to allow arrays to be set with subscript operators. |
|
public short[] |
reverse()Creates a new short array containing items which are the same as this array but in reverse order. |
|
public short[] |
reverse(boolean mutate)Reverses the items in an array. |
|
public short[] |
reverseEach(Closure<?> closure)Iterates through a short[] in reverse order passing each short to the given closure. |
|
public int |
size()Provides arrays with a size method similar to collections. |
|
public Stream<Short> |
stream()Returns a sequential Stream with the specified array as its source. |
|
public short |
sum()Sums the items in an array. |
|
public short |
sum(short initialValue)Sums the items in an array, adding the result to some initial value. |
|
public short[] |
swap(int i, int j)Swaps two elements at the specified positions. |
|
public short[] |
tail()Returns the items from the short array excluding the first item. |
|
public List<Short> |
toList()Converts this array to a List of the same size, with each element added to the list. |
|
public Set<Short> |
toSet()Converts this array to a Set, with each unique element added to the set. |
|
public String |
toString()Returns the string representation of the given array. |
| Methods inherited from class | Name |
|---|---|
class Object |
addShutdownHook, any, any, asBoolean, asType, collect, collect, collect, dump, each, eachMatch, eachMatch, eachWithIndex, every, every, find, find, findAll, findAll, findIndexOf, findIndexOf, findIndexValues, findIndexValues, findLastIndexOf, findLastIndexOf, findResult, findResult, findResult, findResult, getAt, getMetaClass, getMetaPropertyValues, getProperties, grep, grep, hasProperty, identity, inject, inject, inspect, invokeMethod, is, isCase, isNotCase, iterator, metaClass, print, print, printf, printf, println, println, println, putAt, respondsTo, respondsTo, setMetaClass, sleep, sleep, split, sprintf, sprintf, stream, tap, toString, use, use, use, with, with, withCloseable, withCloseable, withMethodClosure, withStream, withStream, withTraits |
Iterates over the contents of a short Array, and checks whether a predicate is valid for at least one element.
short[] array = [0, 1, 2]
assert array.any{ it > 1 }
assert !array.any{ it > 3 }
predicate - the closure predicate used for matchingCoerces a short array to a boolean value. A short array is false if the array is of length 0, and true otherwise.
Calculates the average of the shorts in the array.
assert 5.0G == ([2,4,6,8] as short[]).average()
Chops the short array into pieces, returning lists with sizes corresponding to the supplied chop sizes. If the array isn't large enough, truncated (possibly empty) pieces are returned. Using a chop size of -1 will cause that piece to contain all remaining items from the array.
short[] array = [0, 1, 2]
assert array.chop(1, 2) == [[0], [1, 2]]
chopSizes - the sizes for the returned piecesChecks whether the array contains the given value.
value - the value being searched for Counts the number of occurrences of the given value inside this array.
Comparison is done using Groovy's == operator (using
compareTo(value) == 0).
short[] array = [10, 20, 20, 30]
assert array.count(20) == 2
value - the value being searched forIterates through a short[] passing each short to the given consumer.
short[] array = [0, 1, 2]
String result = ''
array.each{ result += it }
assert result == '012'
consumer - the consumer for each shortIterates through a short[], passing each short and the element's index (a counter starting at zero) to the given closure.
short[] array = [10, 20, 30]
String result = ''
array.eachWithIndex{ item, index -> result += "$index($item)" }
assert result == '0(10)1(20)2(30)'
closure - a Closure to operate on each shortCompares the contents of this array to the contents of the given array.
Example usage:
short[] array1 = [4, 8]
short[] array2 = [4, 8]
assert array1 !== array2
assert array1.equals(array2)
right - the array being comparedIterates over the contents of a short Array, and checks whether a predicate is valid for all elements.
short[] array = [0, 1, 2]
assert array.every{ it < 3 }
assert !array.every{ it > 1 }
predicate - the closure predicate used for matchingReturns the first item from the short array.
short[] shorts = [10, 20, 30]
assert shorts.first() == 10
An alias for head(). Flattens an array. This array is added to a new collection.
It is an alias for toList() but allows algorithms to be written which also
work on multidimensional arrays or non-arrays where flattening would be applicable.
short[] array = [0, 1]
assert array.flatten() == [0, 1]
Supports the subscript operator for a short array with a range giving the desired indices.
short[] array = [1, 3, 5, 7, 9, 11]
assert array[2..<2] == [] // EmptyRange
assert array[(0..5.5).step(2)] == [1, 5, 9] // NumberRange
assert array[(1..5.5).step(2)] == [3, 7, 11] // NumberRange
range - a range indicating the indices for the items to retrieveSupports the subscript operator for a short array with an IntRange giving the desired indices.
short[] array = [0, 10, 20, 30, 40]
assert array[2..3] == [20, 30]
assert array[-2..-1] == [30, 40]
assert array[-1..-2] == [40, 30]
range - an IntRange indicating the indices for the items to retrieveSupports the subscript operator for a short array with an ObjectRange giving the desired indices.
short[] array = [0, 10, 20, 30, 40]
def range = new ObjectRange(2, 3)
assert array[range] == [20, 30]
range - an ObjectRange indicating the indices for the items to retrieveSupports the subscript operator for a short array with a (potentially nested) collection giving the desired indices.
short[] array = [0, 2, 4, 6, 8]
assert array[2, 3] == [4, 6]
assert array[1, 0..1, [0, [-1]]] == [2, 0, 2, 0, 8]
indices - a collection of indices for the items to retrieveReturns indices of the short array.
short[] array = [0, 1]
assert array.indices == 0..1
Returns Groovy's list-like string representation for a short array.
If disabled, e.g. via -Dgroovy.extension.disable=groovyToString(short[]),
the JDK's default array toString() is used instead.
Returns the first item from the short array.
short[] shorts = [10, 20, 30]
assert shorts.head() == 10
An alias for first().Returns the items from the short array excluding the last item.
short[] shorts = [10, 20, 30]
def result = shorts.init()
assert result == [10, 20]
assert shorts.class.componentType == result.class.componentType
Concatenates the string representation of each item in this array.
Concatenates the string representation of each item in this array, with the given String as a separator between each item.
separator - a String separatorReturns the last item from the short array.
short[] shorts = [10, 20, 30]
assert shorts.last() == 30
Returns the index of the partition point according to the given predicate (the index of the first element of the second partition). The arr is assumed to be partitioned according to the given predicate.
def arr = [7, 15, 3, 5, 4, 12, 6] as short[]
assert arr.partitionPoint(0..<arr.size()) { it%2 != 0 } == 4
def arr = [1, 2, 3, 3, 4, 4, 5, 6, 7] as short[]
// usage case like lower_bound(cpp), bisect_left(python)
assert arr.partitionPoint(0..<arr.size()) { it < 4 } == 4
// usage case like upper_bound(cpp), bisect_right(python)
assert arr.partitionPoint(0..<arr.size()) { it <= 4 } == 6
// for all match condition
assert arr.partitionPoint(0..<arr.size()) { it <= 100 } == arr.size()
// for no match condition
assert arr.partitionPoint(0..<arr.size()) { it <= 0 } == 0
// for no match condition with range
assert arr.partitionPoint(2..<arr.size()) { it <= 0 } == 2
range - the range [l,r] to find data match the conditioncondition - the matching conditionReturns the index of the partition point according to the given predicate (the index of the first element of the second partition). The arr is assumed to be partitioned according to the given predicate.
def arr = [7, 15, 3, 5, 4, 12, 6] as short[]
assert arr.partitionPoint{ it%2 != 0 } == 4
def arr = [1, 2, 3, 3, 4, 4, 5, 6, 7] as short[]
// usage case like lower_bound(cpp), bisect_left(python)
assert arr.partitionPoint{ it < 4 } == 4
// usage case like upper_bound(cpp), bisect_right(python)
assert arr.partitionPoint{ it <= 4 } == 6
// for all match condition
assert arr.partitionPoint{ it <= 100 } == arr.size()
// for no match condition
assert arr.partitionPoint{ it <= 0 } == 0
condition - the matching conditionA helper method to allow arrays to be set with subscript operators.
short[] from = [1, 1, 1]
short[] to = [0, 0, 0, 0, 0]
to[1..3] = from
assert to == [0, 1, 1, 1, 0]
range - the subset of the array to setsource - the source array from which new values will comeA helper method to allow arrays to be set with subscript operators. Zero will be used if the source iterable has insufficient elements.
def from = [1, 1, 1]
short[] to = [0, 0, 0, 0, 0]
to[1..3] = from
assert to == [0, 1, 1, 1, 0]
range - the subset of the array to setsource - the source array from which new values will comeCreates a new short array containing items which are the same as this array but in reverse order.
short[] array = 1..2
assert array.reverse() == 2..1
Reverses the items in an array. If mutate is true, the original array is modified in place and returned. Otherwise, a new array containing the reversed items is produced.
short[] array = 1..3
def yarra = array.reverse(true)
assert array == 3..1
assert yarra == 3..1
assert array === yarra
yarra = array.reverse(false)
assert array !== yarra
assert array == 3..1
assert yarra == 1..3
mutate - true if the array itself should be reversed in place, false if a new array should be createdIterates through a short[] in reverse order passing each short to the given closure.
short[] array = [0, 1, 2]
String result = ''
array.reverseEach{ result += it }
assert result == '210'
closure - the closure applied on each short Provides arrays with a size method similar to collections.
Returns a sequential Stream with the specified array as its source.
Stream for the arraySums the items in an array.
assert (1+2+3+4 as short) == ([1,2,3,4] as short[]).sum()
Sums the items in an array, adding the result to some initial value.
assert (5+1+2+3+4 as short) == ([1,2,3,4] as short[]).sum(5 as short)
initialValue - the items in the array will be summed to this initial valueSwaps two elements at the specified positions.
Example:
assert ([1, 3, 2, 4] as short[]) == ([1, 2, 3, 4] as short[]).swap(1, 2)
i - a positionj - a positionReturns the items from the short array excluding the first item.
short[] shorts = [10, 20, 30]
def result = shorts.tail()
assert result == [20, 30]
assert shorts.class.componentType == result.class.componentType
Converts this array to a List of the same size, with each element added to the list.
Converts this array to a Set, with each unique element added to the set.
short[] array = [1, 2, 3, 2, 1]
Set expected = [1, 2, 3]
assert array.toSet() == expected
Returns the string representation of the given array.
short[] array = [1, 2, 3, 2, 1]
assert array.toString() == '[1, 2, 3, 2, 1]'